package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 杨林
 * @create 2022-11-16 14:26 星期三
 * description:
 */
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("{page}/{limit}")
    public R selectPage(@PathVariable Integer page, @PathVariable Integer limit, UserInfoQueryVo userInfoQueryVo){
        Page<UserInfo> userInfoPage = new Page<>(page, limit);
        IPage<UserInfo> pages = userInfoService.selectPage(userInfoPage, userInfoQueryVo);
        return R.ok().data("pageModel",pages);
    }

    @GetMapping("lock/{userId}/{status}")
    public R lock(@PathVariable Long userId,@PathVariable Integer status){
        userInfoService.lock(userId, status);
        return R.ok();
    }

    /**
     * 用户详情
     * @param userId
     * @return
     */
    @GetMapping("show/{userId}")
    public R show(@PathVariable Long userId){
        if (StringUtils.isEmpty(userId)){
            throw new YyghException(20001,"用户id不能为空");
        }
        Map<String, Object> map = userInfoService.show(userId);
        return R.ok().data(map);
    }

    @GetMapping("approval/{userId}/{authStatus}")
    public R approval(@PathVariable long userId,@PathVariable Integer authStatus){
        userInfoService.approval(userId, authStatus);
        return R.ok();
    }
}
